package com.hq.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.hq.pojo.UserInfo;
import com.hq.service.AccountService;
import com.hq.service.UserInfoService;
import com.hq.service.impl.AccountImpl;
import com.hq.service.impl.UserInfoImpl;
import com.hq.utils.JSONObjectUtil;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Login被访问");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		AccountService service = new AccountImpl();
		PrintWriter print = response.getWriter();
		boolean a = service.land(name, password);
		UserInfo userInfo = null;
		//如果登陆成功，添加用户个人信息
		if(a) {
			AccountService accountService = new AccountImpl();
			Integer integer = accountService.getAccountIdByAccountName(name);
			if(integer != null) {
				UserInfoService userInfoService = new UserInfoImpl();
				userInfo = userInfoService.getUserInfoById(integer);
			}
		}
		print.print(JSONObjectUtil.createLoginResult(a,userInfo));
		print.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	

}
